const dayjs = require('dayjs')

export default {
    decode(str) {
        // return str + "1111";

        if (!str) {
            return null;
        }
        let split = str.split(",");
        console.log(split);





        let product = {};
        let now = dayjs().format('YYYY-MM-DD HH:mm:ss')
        let wcode = ""
        let user = wx.getStorageSync('user')
        if (user) {
            wcode = user.wcode
        }


        if (str.includes(":")) {
            let map = {}
            for (const pair of split) {
                let keyValue = pair.split(":");
                if (keyValue.length == 2) {
                    map[keyValue[0]] = keyValue[1]
                }
            }
            product.quality = map["ph"]
            product.itemno = map["lh"]
            product.gslcodex = map["zzjhh"]
            product.sub_fld1 = map["gg"]
            product.skuno = map["kbh"]
            product.sub_fld2 = map["cd"]
            product.sqty = map["zs"]
            product.weight = map["jz"]
            product.busiinvcode = map["hth"]
            product.sdate = now
            product.wcode = wcode
            return product

        }


        if (split.length == 11 || split.length == 12) {

            product.quality = split[2];
            product.itemno = split[3]
            product.gslcodex = split[4]
            product.sub_fld1 = split[5]
            product.skuno = split[6]
            product.sub_fld2 = split[7]
            product.sqty = split[8]
            product.weight = split[9]
            product.busiinvcode = split[10]
            product.sdate = now
            product.wcode = wcode

            if (product.skuno.length == 3) {
                product.skuno = product.gslcodex + product.skuno;
            }
            if (split.length == 11 && product.skuno.length == 3) {
                product.ruletype = "2"
            }
            if (split.length == 11 && product.skuno.length > 3) {
                product.ruletype = "1"
            }
            if (split.length == 12) {
                product.ruletype = "3"
            }

            return product

        }
        // else if (split.length == 10 && str.startsWith("Shijiazhuanglron&SteelCo.")) {
        //     product.quality = split[2];
        //     product.itemno = split[3]
        //     product.gslcodex = "/" //split[4]
        //     product.sub_fld1 = split[5]
        //     product.skuno = "/" //split[6]
        //     product.sub_fld2 = split[6]
        //     product.sqty = split[7]
        //     product.weight = split[8]
        //     product.busiinvcode = split[9]
        //     product.sdate = "2025-----"
        //     product.wcode = "zhf"

        //     return product;

        // } 
        else if (split.length == 9 || split.length == 10) {
            product.quality = split[0]
            product.itemno = split[1]
            product.gslcodex = split[2]
            product.sub_fld1 = split[3]
            product.skuno = split[4]
            product.sub_fld2 = split[5]
            product.sqty = split[6]
            product.weight = split[7]
            product.busiinvcode = ""
            product.sdate = now
            product.wcode = wcode
            product.ruletype = "4"

            return product
        } else {
            return null;
        }

    }
}